package com.zxy.express.service.impl;

import com.autopai.base.common.mybatis.service.impl.BaseServiceImpl;
import com.autopai.base.common.web.constant.Constant;
import com.autopai.base.common.web.convert.ConvertUtils;
import com.autopai.base.common.web.page.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxy.express.dao.RetellReportDao;
import com.zxy.express.dto.RetellReportDTO;
import com.zxy.express.entity.RetellReportEntity;
import com.zxy.express.service.RetellReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 总结复述报告表
 *
 * @author zhangxiying ${email}
 * @since ${version} 2025-07-01
 */
@Service
public class RetellReportServiceImpl extends BaseServiceImpl<RetellReportDao, RetellReportEntity> implements RetellReportService {

    @Override
    public PageData<RetellReportDTO> page(Map<String, Object> params) {
        IPage<RetellReportEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );

        return getPageData(page, RetellReportDTO.class);
    }

    @Override
    public List<RetellReportDTO> list(Map<String, Object> params) {
        List<RetellReportEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, RetellReportDTO.class);
    }

    private QueryWrapper<RetellReportEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<RetellReportEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public RetellReportDTO get(String id) {
        RetellReportEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, RetellReportDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(RetellReportDTO dto) {
        RetellReportEntity entity = ConvertUtils.sourceToTarget(dto, RetellReportEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RetellReportDTO dto) {
        RetellReportEntity entity = ConvertUtils.sourceToTarget(dto, RetellReportEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        //逻辑删除
        //logicDelete(ids, RetellReportEntity.class);

        //物理删除
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

}